package com.example.front;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.example.front.main.MainActivity;
import com.example.front.main.user.UserNameModify;
import com.example.front.sign.LoginActivity;
import com.example.front.sign.User;
import com.squareup.okhttp.Request;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONObject;

/**
 * 欢迎界面
 */
public class WelcomeActivity extends Activity {
    private SharedPreferences sharedPreferences;
    private String account;
    private String password;
    private String url;
    private MyApplication myApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_welcome);

        sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
        if (sharedPreferences == null){
            Intent intent = new Intent(WelcomeActivity.this,LoginActivity.class);
            startActivity(intent);
            finish();
        }
        else {
            account = sharedPreferences.getString("account", "");
            password = sharedPreferences.getString("password","");
            if (account == ""){
                Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
            else if(password == ""){
                Intent intent = new Intent(WelcomeActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
            else {
                myApplication = (MyApplication) getApplication();
                url = myApplication.getURL();

                OkHttpUtils
                        .post()
                        .url(url + "/user/login")
                        .addParams("account", account)
                        .addParams("password", password)
                        .build()
                        .execute(new StringCallback() {
                            @Override
                            public void onError(Request request, Exception e) {
                                if(e.toString().contains("java.net.UnknownHostException")){
                                    AlertDialog dialog = new AlertDialog.Builder(WelcomeActivity.this)
                                            .setTitle("提示：")
                                            .setMessage("网络连接失败，请检查网络连接")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    finish();
                                                }
                                            })
                                            .create();
                                    dialog.show();
                                }
                                else if(e.toString().contains("java.lang.RuntimeException")){
                                    AlertDialog dialog = new AlertDialog.Builder(WelcomeActivity.this)
                                            .setTitle("提示：")
                                            .setMessage("服务器正在维护，请稍后再来哦！")
                                            .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                                @Override
                                                public void onClick(DialogInterface dialog, int which) {
                                                    finish();
                                                }
                                            })
                                            .create();
                                    dialog.show();
                                }
                            }

                            @Override
                            public void onResponse(String response) {
                                if (response.equals("true")){
                                    WelcomeActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            OkHttpUtils
                                                    .get()
                                                    .url(url + "/user/getUserInformation/" + account)
                                                    .build()
                                                    .execute(new StringCallback() {
                                                        @Override
                                                        public void onError(Request request, Exception e) {
                                                            WelcomeActivity.this.runOnUiThread(new Runnable() {
                                                                @Override
                                                                public void run() {
                                                                    showDialog();
                                                                }
                                                            });
                                                        }

                                                        @Override
                                                        public void onResponse(String response) {
                                                            String json = response;
                                                            System.out.println(json);
                                                            try {
                                                                JSONObject jsonObject = new JSONObject(json);
                                                                User user = new User();
                                                                SharedPreferences sharedPreferences = getSharedPreferences("Login", Context.MODE_PRIVATE);
                                                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                                                editor.putBoolean("LoginBool", true);
                                                                editor.putString("photo", jsonObject.getString("photo"));
                                                                editor.putString("account", jsonObject.getString("account"));
                                                                editor.putString("username", jsonObject.getString("username"));
                                                                editor.putInt("sex", jsonObject.getInt("sex"));
                                                                editor.putString("password",jsonObject.getString("password"));
                                                                editor.putString("phone", jsonObject.getString("phone"));
                                                                if (jsonObject.getString("email").equals("")){
                                                                    editor.putString("email", "");
                                                                }
                                                                else {
                                                                    editor.putString("email", jsonObject.getString("email"));
                                                                }
                                                                editor.apply();
                                                            }catch (Exception e){
                                                                e.printStackTrace();
                                                            }
                                                            Intent intent = new Intent(WelcomeActivity.this, MainActivity.class);
                                                            startActivity(intent);
                                                            finish();
                                                        }
                                                    });
                                        }
                                    });
                                }
                                else {
                                    WelcomeActivity.this.runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Toast.makeText(WelcomeActivity.this, "用户信息已过期，请重新登录", Toast.LENGTH_SHORT).show();
                                            Intent intent = new Intent(WelcomeActivity.this,LoginActivity.class);
                                            startActivity(intent);
                                            finish();
                                        }
                                    });
                                }
                            }
                        });
            }
        }

    }

    //弹出框
    private void showDialog() {
        AlertDialog dialog = new AlertDialog.Builder(this)
                .setTitle("提示：")
                .setMessage("网络连接失败，请检查网络连接")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        finish();
                    }
                })
                .create();
        dialog.show();
    }
}